//
//  CZPullDownRefresh.swift
//  sinaWeiboswift
//
//  Created by 段文杰 on 16/4/16.
//  Copyright © 2016年 段文杰. All rights reserved.
//

import UIKit
private let CZPullDownRefreshControlHeight: CGFloat = 60

enum CZPullDownRefreshControlStatus: Int {
    case Normal // 箭头朝下, 下拉刷新
    case PullToRefresh // 箭头朝上, 释放刷新
    case Refreshing // 正在刷新
}

class CZPullDownRefreshView: UIView {

   
    override init(frame: CGRect) {
        let newFrame = CGRect(x: 0, y: -CZPullDownRefreshControlHeight, width: UIScreen.mainScreen().bounds.width, height:CZPullDownRefreshControlHeight )
        
        super.init(frame: newFrame)
        self.backgroundColor = UIColor.init(red: ( CGFloat(arc4random_uniform(256)) / 255.0), green: ( CGFloat(arc4random_uniform(256)) / 255.0), blue: ( CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(newSuperview)
        if let scrollView = newSuperview as? UIScrollView {
            superScoroView = scrollView
            
            
            //KVO监听值得改变
            
            superScoroView?.addObserver(self, forKeyPath: "contentOffset", options: [], context: nil)
        }
    }
    
    //移除监听
    deinit{
    removeObserver(self, forKeyPath: "contentOffset")
    
    }
    
    //监听方法
 

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        HMPrint("滚动了\(superScoroView?.contentOffset.y)")

    }
    
    var status : CZPullDownRefreshControlStatus = CZPullDownRefreshControlStatus.Normal
    private var superScoroView : UIScrollView?
}
